{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### OCR晨报识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-16T00:40:22.923431Z",
     "start_time": "2019-08-16T00:40:19.661151Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "\n",
      "\t\n",
      "\n",
      "11\n",
      "\n",
      "39\n",
      "\n",
      "119\n",
      "\n",
      "39\n",
      "\n",
      "111\n",
      "\n",
      "18\n",
      "\n",
      "12\n",
      "\n",
      "\t\n",
      "\n",
      "11\n",
      "58\n",
      "37\n",
      "20\n",
      "18\n",
      "219\n",
      "\n",
      "13\n",
      "547\n",
      "464\n",
      "\n",
      "97\n",
      "437\n",
      "\n",
      "21\n",
      "1147\n",
      "\n",
      "4l\n",
      "323\n",
      "\n",
      "86\n",
      "188\n",
      "\n",
      "39\n",
      "330\n",
      "\n",
      "11\n",
      "\n",
      "360\n",
      "\n",
      "119\n",
      "\n",
      "61\n",
      "521\n",
      "\n",
      "29\n",
      "\n",
      "\t\n",
      "\n",
      "528\n",
      "344\n",
      "116\n",
      "\n",
      "93\n",
      "2005\n",
      "\n",
      "245\n",
      "\n",
      "163\n",
      "444\n",
      "\n",
      "545\n",
      "279\n",
      "1032\n",
      "\n",
      "350\n",
      "329\n",
      "1218\n",
      "\n",
      "250\n",
      "1340\n",
      "\n",
      "258\n",
      "362\n",
      "492\n",
      "425\n",
      "\n",
      "822\n",
      "193\n",
      "548\n",
      "242\n",
      "156\n",
      "280\n",
      "167\n",
      "1659\n",
      "\n",
      "77\n",
      "194\n",
      "\n",
      "\t\n",
      "\n",
      "17\n",
      "\n",
      "76\n",
      "\n",
      "81\n",
      "\n",
      "65\n",
      "141\n",
      "\n",
      "0\n",
      "\n",
      "51\n",
      "\n",
      "73\n",
      "\n",
      "54\n",
      "74\n",
      "\n",
      "0\n",
      "\n",
      "8363\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "123\n",
      "\n",
      "\t\n",
      "\n",
      "76\n",
      "18\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "95\n",
      "123\n",
      "136\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "20\n",
      "91\n",
      "17\n",
      "435\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "51\n",
      "89\n",
      "53\n",
      "242\n",
      "144\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "53\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "22\n",
      "48\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "143\n",
      "\n",
      "\t\n",
      "\n",
      "\t\n",
      "\n",
      "35\n",
      "30919\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<os._wrap_close at 0x19ab201f320>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别前处理\n",
    "# 图片二值化\n",
    "from PIL import Image\n",
    "import os\n",
    "os.chdir('D:\\OCR')\n",
    "img = Image.open('3.png')\n",
    " \n",
    "# 模式L”为灰色图像，它的每个像素用8个bit表示，0表示黑，255表示白，其他数字表示不同的灰度。\n",
    "Img = img.convert('L')\n",
    "Img.save(\"test1.png\")\n",
    " \n",
    "# 自定义灰度界限，大于这个值为黑色，小于这个值为白色\n",
    "threshold = 200\n",
    " \n",
    "table = []\n",
    "for i in range(256):\n",
    "    if i < threshold:\n",
    "        table.append(0)\n",
    "    else:\n",
    "        table.append(1)\n",
    " \n",
    "# 图片二值化\n",
    "photo = Img.point(table, '1')\n",
    "photo.save(\"test2.png\")\n",
    "# 识别图片内容\n",
    "import pytesseract\n",
    "img_path = 'test2.png'\n",
    "\n",
    "text=pytesseract.image_to_string(Image.open(img_path))\n",
    "\n",
    "# # 用tab代替空格,便于粘到excel可以自动分列\n",
    "text = text.replace(' ','\\t')\n",
    "# province = ['上海市','云南省','内蒙古自治区','北京市','吉林省','四川省','天津市','宁夏回族自治区',\n",
    "# '安徽省','山东省','山西省','广东省','广西壮族自治区','新疆维吾尔自治区','江苏省','江西省',\n",
    "# '河北省','河南省','浙江省','海南省','湖北省','湖南省','甘肃省','福建省','西藏自治区','贵州省',\n",
    "# '辽宁省','重庆市','陕西省','青海省','黑龙江']\n",
    "\n",
    "# for p in province:\n",
    "#     text = text.replace('TIANCHENG',p,1)\n",
    "with open('text.txt','w+') as f:\n",
    "    f.write(text)\n",
    "print(text)\n",
    "os.popen(r'C:\\Program Files (x86)\\Notepad++\\notepad++')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
